package com.example.innospace.demos.web.service.Impl;

import com.example.innospace.demos.pojo.dto.TeacherDTO;
import com.example.innospace.demos.pojo.dto.TeacherPageQueryDTO;
import com.example.innospace.demos.pojo.entity.Teacher;
import com.example.innospace.demos.pojo.result.PageResult;
import com.example.innospace.demos.web.mapper.TeacherMapper;
import com.example.innospace.demos.web.service.TeacherService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;
    @Override
    public String getpassword(String teacherId) {
        return teacherMapper.getpassword(teacherId);
    }

    @Override
    public void updatePassword(String newPassword, String teacherId) {
        teacherMapper.updatePassword(newPassword, teacherId);
    }

    @Override
    public Teacher findById(String teacherId) {
        return teacherMapper.findById(teacherId);
    }

    @Override
    public void insert(TeacherDTO teacherDTO) {
        teacherDTO.setAvatar("https://spacea.oss-cn-beijing.aliyuncs.com/14a40fac-4185-4d94-824f-efe83fcb60f0-195.jpg");
        teacherDTO.setPassword("ksd@"+teacherDTO.getTeacherId());
        teacherDTO.setStatus(1);
        teacherMapper.insert(teacherDTO);
    }

    @Override
    public boolean batchDeleteById(List<String> teacherIds) {
        for(int i=0;i<teacherIds.size();i++)
        {
            Teacher teacher=teacherMapper.findById(teacherIds.get(i));
            if(teacher.getStatus()==1)
                return false;
        }
        teacherMapper.batchDeleteById(teacherIds);
        return true;
    }

    @Override
    public void updateTeacherInfo(String teacherId, Teacher teacher) {
        teacherMapper.updateTeacherInfo(teacherId, teacher);
    }

    @Override
    public void resetTeacherPassword(String teacherId) {
        Teacher teacher=teacherMapper.findById(teacherId);
        String password="ksd@"+teacher.getTeacherId();
        teacherMapper.updatePassword(password,teacherId);
    }

    @Override
    public void teacherstartOrstop(Integer status, String teacherId) {
        teacherMapper.teacherstartOrstop(status,  teacherId);
    }

    @Override
    public PageResult pageQuery(int pageNum, int pageSize, TeacherPageQueryDTO teacherPageQueryDTO) {
      PageHelper.startPage(pageNum,pageSize);
        Page<Teacher> page=teacherMapper.pageQuery(teacherPageQueryDTO);
        long total=page.getTotal();
        List<Teacher> records=page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public void updateAvatar(String teacherId, String fileUrl) {
teacherMapper.updateAvatar(teacherId,fileUrl);
    }

    @Override
    public String getAvatar(String teacherId) {
        return teacherMapper.getAvatar(teacherId);
    }
}
